package com.ruoyi.web.controller.datastatistics;

import java.text.ParseException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.ruoyi.basedata.domain.ElectricityTerminalsXb;
import com.ruoyi.basedata.mapper.ElectricityTerminalsXbMapper;
import com.ruoyi.datastatistics.domain.ElectricityMeterHistoryData;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.datastatistics.domain.ElectricityMeterHistoryData;
import com.ruoyi.datastatistics.service.IElectricityMeterHistoryDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 电-历史数据Controller
 *
 * @author gdqt
 * @date 2025-03-10
 */
@RestController
@RequestMapping("/datastatistics/electricityMeterHistory")
public class ElectricityMeterHistoryDataController extends BaseController
{
    @Autowired
    private IElectricityMeterHistoryDataService meterHistoryDataService;

    @Autowired
    private ElectricityTerminalsXbMapper terminalsXbService;
    /**
     * 查询小-历史数据列表
     */
//    @PreAuthorize("@ss.hasPermi('datastatistics:meterhistory:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(ElectricityMeterHistoryData meterHistoryData) throws ParseException {
//        startPage();
//        List<ElectricityMeterHistoryData> list = meterHistoryDataService.selectMeterHistoryDataList(meterHistoryData);
//        return getDataTable(list);
//    }


    /**
     * 查询小-历史数据列表
     */
    @GetMapping("/list")
    public TableDataInfo getList(ElectricityMeterHistoryData meterHistoryData) throws ParseException {
        startPage();
        List<ElectricityMeterHistoryData> list = meterHistoryDataService.selectList(meterHistoryData);
        return getDataTable(list);
    }

    /**
     * 导出小-历史数据列表
     */
    @Log(title = "小-历史数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElectricityMeterHistoryData meterHistoryData) throws ParseException {


        ElectricityTerminalsXb terminalsXb = new ElectricityTerminalsXb();
        terminalsXb.setLocationNb(meterHistoryData.getLocationNb());
        terminalsXb.setTerminalMark(1);
        List<ElectricityTerminalsXb> terminalsXbs = terminalsXbService.selectTerminalsXbList(terminalsXb);
        if (CollectionUtils.isNotEmpty(terminalsXbs)){
            terminalsXb = terminalsXbs.get(0);
        }
        List<ElectricityMeterHistoryData> list = meterHistoryDataService.selectList(meterHistoryData);
        ElectricityTerminalsXb finalTerminalsXb = terminalsXb;
        list.forEach(data ->{
            data.setUserName(finalTerminalsXb.getUserName());
            data.setUserId(finalTerminalsXb.getUserId());
            data.setUserAddress(finalTerminalsXb.getUserAddress());
        });
        ExcelUtil<ElectricityMeterHistoryData> util = new ExcelUtil<ElectricityMeterHistoryData>(ElectricityMeterHistoryData.class);
        util.exportExcel(response, list, "小-历史数据数据");
    }

    /**
     * 获取小-历史数据详细信息
     */
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(meterHistoryDataService.selectMeterHistoryDataByID(ID));
    }

    /**
     * 新增小-历史数据
     */
    @Log(title = "小-历史数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ElectricityMeterHistoryData meterHistoryData) {
        return toAjax(meterHistoryDataService.insertMeterHistoryData(meterHistoryData));
    }



    /**
     * 新增小-历史和实时数据
     */
    @InterceptorIgnore(tenantLine = "true")
    @Log(title = "小-历史数据", businessType = BusinessType.INSERT)
    @PostMapping("/addHisandRealtimeData")
    public AjaxResult addHisandRealtimeData(@Validated @RequestBody ElectricityMeterHistoryData meterHistoryData) {
        return meterHistoryDataService.insertMeterHistoryDataAndRealData(meterHistoryData);
    }





    /**
     * 修改小-历史数据
     */
    @Log(title = "小-历史数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ElectricityMeterHistoryData meterHistoryData) {
        return toAjax(meterHistoryDataService.updateMeterHistoryData(meterHistoryData));
    }

    /**
     * 删除小-历史数据
     */
    @Log(title = "小-历史数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs) {
        return toAjax(meterHistoryDataService.deleteMeterHistoryDataByIDs(IDs));
    }

}
